import { action, observable } from 'mobx'
import { Api } from '@/helper/http'
import SSRStore from '@/helper/mobx/SSRStore'
import cookie from 'react-cookies'
export default class extends SSRStore {
  @observable user = null
  @observable token = null
  @action loadUser() {
    this.user = cookie.load('user')
  }
  @action logout() {
    cookie.remove('user')
    cookie.remove('token')
    cookie.remove('accessToken')
    this.user = null
  }
  @action async authByfacebook(data) {
    const accessToken = data.tokenDetail.accessToken
    const { user, token, expiresIn } = await Api.post(`/user/gateway/facebookAuth`, { accessToken })
    cookie.save('user', user, { expiresIn })
    cookie.save('token', token, { expiresIn })
    cookie.save('accessToken', accessToken, { expiresIn })
    this.user = user
  }
}
